﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using RVBike.Properties;

namespace RVBike
{
    /// <summary>
    /// Interaction logic for MainPage.xaml
    /// </summary>
    public partial class MainPage : UserControl, ISwitchable
    {
        private int _playerIndex;

        public MainPage()
        {
            InitializeComponent();

            _playerIndex = -1;

            // Multi-language string
            dummy_Name.Content = MultiLanguage.Instance().Translate("user_name");
            dummy_password.Content = MultiLanguage.Instance().Translate("user_password");
            dummy_login.Content = MultiLanguage.Instance().Translate("login_needed");
            btn_register.Content = MultiLanguage.Instance().Translate("register");
            btn_login.Content = MultiLanguage.Instance().Translate("login");
            btn_logout.Content = MultiLanguage.Instance().Translate("logout");
            btn_single.Content = MultiLanguage.Instance().Translate("single_game");
            btn_multiple.Content = MultiLanguage.Instance().Translate("multi_game");
            btn_setting.Content = MultiLanguage.Instance().Translate("setting");
            btn_exit.Content = MultiLanguage.Instance().Translate("exit");

            UpdatePage();
        }

        private void UpdatePage()
        {
            text_name.Text = UserSetting.Instance().UserName;
            text_password.Password = UserSetting.Instance().Password;
            check_login.IsChecked = UserSetting.Instance().LoginNeeded;

            Character aChar = CharacterManager.Instance().GetCharByName(UserSetting.Instance().CharName);
            if (aChar == null)
            {
                // Assign a default character (first index)
                aChar = CharacterManager.Instance().GetCharByIndex(0);
                UserSetting.Instance().CharName = aChar.Name;
            }

            _playerIndex = CharacterManager.Instance().GetCharIndex(aChar.Name);
            image_player.Source = new BitmapImage(new Uri(aChar.ImagePath));

            if ((bool)check_login.IsChecked)
            {
                text_password.IsEnabled = true;
                btn_login.IsEnabled = true;
                btn_logout.IsEnabled = true;
            }
            else
            {
                text_password.IsEnabled = false;
                btn_login.IsEnabled = false;
                btn_logout.IsEnabled = false;
            }
        }

        private void OnButtonClick(object sender, RoutedEventArgs e)
        {
            if (sender == btn_exit)
            {
                RVSwitcher.Switch(new WelcomePage());
            }
            else if (sender == btn_setting)
            {
                SettingWindow settingWindow = new SettingWindow();
                settingWindow.Title = MultiLanguage.Instance().Translate("setting");
                settingWindow.Left = Application.Current.MainWindow.Left + Application.Current.MainWindow.Width / 2 -
                    settingWindow.Width/2;
                settingWindow.Top = Application.Current.MainWindow.Top + Application.Current.MainWindow.Height / 2 -
                    settingWindow.Height / 2;

                if (settingWindow.ShowDialog().Value)
                {
                    // Language changed!!
                    if (settingWindow.SelectedLanguage != Settings.Default.Locale)
                    {
                        MessageBox.Show(MultiLanguage.Instance().Translate("please_restart_application"));
                        UserSetting.Instance().Locale = settingWindow.SelectedLanguage;
                        Application.Current.Shutdown();
                    }
                }
            }
            else if (sender == btn_single)
            {
                RVSwitcher.Switch(new SinglePlayerPage(false));
            }
            else if (sender == btn_multiple)
            {
                RVSwitcher.Switch(new MultiplePlayerPage());
            }
            else if (sender == btn_player_l)
            {
                --_playerIndex;
                if (_playerIndex < 0)
                {
                    _playerIndex = CharacterManager.Instance().GetCharacterCount() - 1;
                }
                UserSetting.Instance().CharName = CharacterManager.Instance().GetCharByIndex(_playerIndex).Name;
                UpdatePage();
            }
            else if (sender == btn_player_r)
            {
                ++_playerIndex;
                if (_playerIndex >= CharacterManager.Instance().GetCharacterCount())
                {
                    _playerIndex = 0;
                }
                UserSetting.Instance().CharName = CharacterManager.Instance().GetCharByIndex(_playerIndex).Name;
                UpdatePage();
            }
            else if (sender == check_login)
            {
                UserSetting.Instance().LoginNeeded = (bool)check_login.IsChecked;
                UpdatePage();
            }
            else if (sender == btn_register)
            {
                ;
            }
            else if (sender == btn_login)
            {
                ;
            }
            else if (sender == btn_logout)
            {
                ;
            }
        }

        #region ISwitchable Members
        public void UtilizeState(object state)
        {
            throw new NotImplementedException();
        }

        public void WindowLocationChanged(double x, double y)
        {
        }
        #endregion

        private void text_name_TextChanged(object sender, TextChangedEventArgs e)
        {
            UserSetting.Instance().UserName = text_name.Text;
        }

        private void text_password_PasswordChanged(object sender, RoutedEventArgs e)
        {
            UserSetting.Instance().Password = text_password.Password;
        }
    }
}
